package com.fallsvc.chatroom.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

/**
 * @auther falls_vc
 * description:
 * @date 2025/4/30  20:41
 */
@Slf4j
@Component
public class TestWebSocketAPI extends TextWebSocketHandler {
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        // websocket连接建立后自动调用
        log.info("websocket连接建立成功");
        super.afterConnectionEstablished(session);
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        // 收到信息时被自动调用
        log.info("收到信息message:{}",message);
        super.handleTextMessage(session, message);
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        log.info("发生异常 e:{}",exception);
        //  发生异常时被自动调用
        super.handleTransportError(session, exception);
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        // 正常关闭后调用
        log.info("websocket连接关闭");
        super.afterConnectionClosed(session, status);
    }
}
